import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

/*
  Generated class for the MapsProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
declare let BMap;//百度地图
declare let BMAP_STATUS_SUCCESS;//百度地图的状态

@Injectable()
export class MapsProvider {

  constructor(public http: HttpClient) {
    console.log('Hello MapsProvider Provider');
  }

  /**
	 * 初始化地图
	 * @param id 
	 */
  public initMap(id: any) {
    return new BMap.Map(id, {
      resizeEnable: true,
      zoom: 8,
      center: [116.39, 39.9]
    });
  }

  /**
   * 定位操作
   * //关于状态码
	*  //BMAP_STATUS_SUCCESS	检索成功。对应数值“0”。
	* //BMAP_STATUS_CITY_LIST	城市列表。对应数值“1”。
  *	//BMAP_STATUS_UNKNOWN_LOCATION	位置结果未知。对应数值“2”。
  *	//BMAP_STATUS_UNKNOWN_ROUTE	导航结果未知。对应数值“3”。
  *	//BMAP_STATUS_INVALID_KEY	非法密钥。对应数值“4”。
  *	//BMAP_STATUS_INVALID_REQUEST	非法请求。对应数值“5”。
  *	//BMAP_STATUS_PERMISSION_DENIED	没有权限。对应数值“6”。(自 1.1 新增)
  *	//BMAP_STATUS_SERVICE_UNAVAILABLE	服务不可用。对应数值“7”。(自 1.1 新增)
  *	//BMAP_STATUS_TIMEOUT	超时。对应数值“8”。(自 1.1 新增)
   */
  public doGeolocation(map: any) {
    //定位操作
    let that = this;
    let geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (result) {
      if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        let marker = that.addMarker(map, result.point);
        that.setMarker(marker,
          result.address.province +
            result.address.city +
            result.address.district +
            result.address.street +
            (result.address.streetNumber == undefined ? "" : result.address.streetNumber));
        map.centerAndZoom(result.point, 16);
      }
    });
    geolocation.enableSDKLocation();//开启SDK辅助定位
  }

  /**
   * 逆地理编码
   */
  public doLocation(point: any, marker: any) {
    let that = this;
    // 创建地理编码实例      
    let myGeo = new BMap.Geocoder();
    // 根据坐标得到地址描述    
    myGeo.getLocation(point, function (result) {
      if (result) {
        that.setMarker(marker,
          result.addressComponents.province +
            result.addressComponents.city +
            result.addressComponents.district +
            result.addressComponents.street +
            (result.addressComponents.streetNumber == undefined ? "" : result.addressComponents.streetNumber));
      }
    });
  }

  /**
   * 添加标注
   */
  public addMarker(map: any, point: any) {
    let marker = new BMap.Marker(point);
    map.addOverlay(marker);
    let icon = marker.getIcon();
    icon.setSize(new BMap.Size(60, 60));
    marker.setIcon(icon);
    return marker;
  }

  /**
 * 添加标注
 */
  public setMarker(marker: any, title: any) {
    var label = new BMap.Label(title, { offset: new BMap.Size(-60, -40) });
    marker.setLabel(label);
  }

  /**
   * 推拽事件
   */
  public dragEvent(map: any, callback: any) {
    let that = this;
    map.addEventListener("dragend", function () {
      let point = map.getCenter();
      map.clearOverlays();
      let marker = that.addMarker(map, point);
      if (callback)
        callback(point, marker);
    });
  }
}
